package com.hns.can.base.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.hns.can.base.entity.*;
import com.hns.can.base.vo.AuthorityFuncParamVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.hns.can.base.service.SysAuthorityFuncService;
import com.hns.can.base.service.SysUserService;
import com.hns.can.base.vo.SysAuthorityFuncVo;
import com.hns.can.common.entity.BaseEntity;
import com.hns.can.common.web.BaseController;
import com.hns.frame.util.SpringContextUtil;

/**
 * 功能权限
 * @author:Fisher
 * @email:zhuangcaijin@126.com
 * @version Revision 2.0.0
 */
@Controller
@SessionAttributes( {"SES_THIRDMENU"})
@RequestMapping("/sysAuthorityFunc")
public class SysAuthorityFuncController extends BaseController{
    @Autowired
    private SysAuthorityFuncService sysAuthorityFuncService;
    @Autowired
    private SysUserService sysUserService; // 注入sysUserService


    /**
     * @author:zy
     * @email:zhangyu2222z@sina.cn
     * @创建日期:2014-6-9
     * @功能说明：获取功能权限树信息
     */
    @RequestMapping("/getFuncTreeData")
    @ResponseBody
    public Object getFuncTreeData(String roleId, String level) {
        List<SysAuthorityFuncVo> list = sysAuthorityFuncService.getFuncTreeData(roleId, level);
        return this.getReturnResult(list);
    }

    /**
     * @author:zy
     * @email:zhangyu2222z@sina.cn
     * @创建日期:2014-6-9
     * @功能说明：获取数据权限树信息
     */
    @RequestMapping("/getResTreeData")
    @ResponseBody
    public Object getResTreeData(String roleId, String corpId, String level, String roleType, String selResType) {
        List<BaseEntity> list = sysAuthorityFuncService.getResTreeData(roleId, corpId, level, roleType, selResType);
        return this.getReturnResult(list);
    }

    /**
     * @author:zy
     * @email:zhangyu2222z@sina.cn
     * @创建日期:2014-6-9
     * @功能说明：获取已授权的功能
     */
    @RequestMapping("/getCheckedFuncData")
    @ResponseBody
    public Object getCheckedFuncData(@RequestParam("roleId")String roleId){
        List<SysAuthorityFuncVo> list = sysAuthorityFuncService.getCheckedFuncData(roleId);
        return this.getReturnResult(list);
    }


    /**
     * @author:zy
     * @email:zhangyu2222z@sina.cn
     * @创建日期:2017-3-17
     * @功能说明：获取已授权的功能id
     */
    @RequestMapping("/getCheckedFuncCache")
    @ResponseBody
    public Object getCheckedFuncCache(){
        Set<SysRoleEntity> setRole = this.getCurrentUser().getSet_role();
        List<SysAuthorityFuncVo> list = sysAuthorityFuncService.getCheckedFuncCache(setRole);
        return this.getReturnResult(list);
    }


    /**
     * @author:zy
     * @email:zhangyu2222z@sina.cn
     * @创建日期:2014-6-13
     * @功能说明：获取已授权的数据
     */
    @RequestMapping("/getCheckedResData")
    @ResponseBody
    public Object getCheckedResData(@RequestParam("roleId")String roleId){
        List<SysAuthorityResourceEntity> list = sysAuthorityFuncService.getCheckedResData(roleId);
        return this.getReturnResult(list);
    }

    /**
     * @author:zy
     * @email:zhangyu2222z@sina.cn
     * @创建日期:2014-6-10
     * @功能说明：编辑指定角色对应功能、按钮权限和字段权限
     */
    @RequestMapping("/saveFuncAuthority")
    @ResponseBody
    public Object saveFuncAuthority(@RequestParam(value = "funcIds",required = false) String[] funcIds,
        @RequestParam(value = "buttonIds",required = false) String[] buttonIds,
        @RequestParam(value = "fieldIds",required = false) String[] fieldIds,//字段id
        @RequestParam(value = "fieldAliases",required = false) String[] fieldAliases,//字段别名
        @RequestParam(value = "roleId",required = false) String roleId,
        @RequestParam(value = "ufuncId",required = false) String ufuncId){
        sysAuthorityFuncService.saveAuthority(funcIds, buttonIds, fieldIds, fieldAliases, roleId, ufuncId);
        sysAuthorityFuncService.cancelAuthorityFunc(roleId, ufuncId);
        SpringContextUtil.updateResMap();
        return getReturnResult();
    }


    /**
     * @author:zy
     * @email:zhangyu2222z@sina.cn
     * @创建日期:2014-6-13
     * @功能说明：编辑指定角色对应数据权限
     */
    @RequestMapping("/saveResAuthority")
    @ResponseBody
//    public Object saveResAuthority(@RequestParam(value="dataIds",required=false)String[] dataIds,
//        @RequestParam(value="isFulls",required=false)String[] isFulls,
//        @RequestParam(value="dataType",required=false)String dataType,
//        @RequestParam(value="roleId",required=false) String roleId){
//    	roleId = request.getParameter("roleId");
    public Object saveResAuthority(String[] dataIds, String[] isFulls, String dataType, String roleId){
        sysAuthorityFuncService.saveResAuthority(dataIds, isFulls, dataType, roleId);
        sysAuthorityFuncService.cancelAuthorityRes(roleId);
        return getReturnResult();
    }

    /**
     * @author:JeffXu
     * @email:835264047@qq.com
     * @创建日期:2015-3-13
     * @功能说明：获取当前用户所拥有的资源权限数据
     */
    @RequestMapping("/getCurrentAccessResData")
    @ResponseBody
    public Object getCurrentAccessResData(String roleId, String level) throws Exception {
        List<SysFieldValueRegisterEntity> list = sysAuthorityFuncService.getCurrentAccessResData(roleId, level);
        return this.getReturnResult(list);
    }

    /**
     * @author:JeffXu
     * @email:835264047@qq.com
     * @创建日期:2015-3-13
     * @功能说明：获取选中角色所拥有的资源权限数据
     */
    @RequestMapping("/getCheckedAccessResData")
    @ResponseBody
    public Object getCheckedAccessResData(String roleId) throws Exception {
        List<SysFieldValueRegisterEntity> list = sysAuthorityFuncService.getCheckedAccessResData(roleId);
        return this.getReturnResult(list);
    }

    /**
     * @param dataIds
     * @param isFulls
     * @param dataType
     * @param roleId
     * @param request
     * @param response
     * @author:JeffXu
     * @email:835264047@qq.com
     * @创建日期:2015-3-16
     * @功能说明：编辑指定角色对应资源权限
     */
    @RequestMapping("/saveAccessResAuthority")
    @ResponseBody
    public Object saveAccessResAuthority(String roleId, String[] fieldValueIds) {
        sysAuthorityFuncService.saveAccessResAuthority(roleId, fieldValueIds);
        sysAuthorityFuncService.cancelAuthorityFieldValue(roleId);
        return getReturnResult();
    }

    /**
     * @author:JeffXu
     * @email:835264047@qq.com
     * @创建日期:2015-3-13
     * @功能说明：获取当前用户所拥有的字段权限数据
     */
    @RequestMapping("/getCurrentFieldData")
    @ResponseBody
    public Object getCurrentFieldData(String funcId, String roleId, String level) throws Exception {
        List<SysFieldRegisterEntity> list = sysAuthorityFuncService.getCurrentFieldData(funcId, roleId, level);
        return getReturnResult(list);
    }

    /**
     * @author:JeffXu
     * @email:835264047@qq.com
     * @创建日期:2015-3-13
     * @功能说明：获取选中角色所拥有的字段权限数据
     */
    @RequestMapping("/getCheckedFieldData")
    @ResponseBody
    public Object getCheckedFieldData(String roleId, String funcId) throws Exception {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("roleId", roleId);
        paramMap.put("funcId", funcId);

        List<SysFieldRegisterEntity> list = sysAuthorityFuncService.getCheckedFieldData(paramMap);
        return getReturnResult(list);
    }

    /**
     * @author:cwm
     * @email:112362667@qq.com
     * @创建日期:2015-3-17
     * @功能说明：取得用户授权字段信息
     */
    @RequestMapping("/getAuthField")
    @ResponseBody
    public Object getAuthField(String tableId) throws Exception {
    	SysUserEntity user = this.getCurrentUser().getUser();
    	List<SysAuthrityFieldEntity> list = sysAuthorityFuncService.getAuthField(user.getId(), tableId);
    	return this.getReturnResult(list);
    }

    /**
     * @author:zy
     * @email:zhangyu2222z@sina.cn
     * @创建日期:2017-3-24
     * @功能说明：获取拥有字段权限设置的功能表
     */
    @RequestMapping("/getAuthFuncList")
    @ResponseBody
    public Object getAuthFuncList(AuthorityFuncParamVo params){
        List<SysFuncRegisterEntity> authFuncList = sysAuthorityFuncService.getAuthFuncList(params);
        return this.getReturnResult(authFuncList);
    }

    /**
     * @author:zy
     * @email:zhangyu2222z@sina.cn
     * @创建日期:2017-3-24
     * @功能说明：保存字段权限设置的功能表
     */
    @RequestMapping("/saveFieldAuthority")
    @ResponseBody
    public Object saveFieldAuthority(AuthorityFuncParamVo params){
        sysAuthorityFuncService.saveFieldAuthority(params);
        return this.getReturnResult();
    }

    /**
     * @功能说明：获取功能权限树
     * @param: params
     * @return
     * @author: zhangy
     * @email: zhangy2222z@sina.cn
     */
    @RequestMapping("/getFuncDataResList")
    @ResponseBody
    public Object getFuncDataResList(AuthorityFuncParamVo params) {
        params.setRoleType(this.getCurrentUser().getUser().getRoleType());
        List<SysAuthorityFuncVo> funcDataResList = sysAuthorityFuncService.getFuncDataResList(params);
        return this.getReturnResult(funcDataResList);
    }

}
